// pages/hotelRoom/hotelRoom.js
const app = getApp()

const { extend, Field, Switch, Stepper, Toast, Actionsheet } = require('../../zanui/index')
import { obj2Form } from '../../utils/util.js'

Page(extend({}, Field, Switch, Stepper, Toast, Actionsheet, {

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    submitDisabled: false,
    belongHotelId: null,
    roomFacilities: [],
    networkTypes: [
      { value: 0, checked: true, name: '免费WIFI' },
      { value: 1, checked: false, name: '免费宽带' },
      { value: 2, checked: false, name: '收费WIFI' },
      { value: 3, checked: false, name: '收费宽带' },
      { value: 4, checked: false, name: '收费标准' }
    ],
    isShowNetworkPrice: false,//是否显示收费输入框
    roomName: '',
    roomArea: '',
    roomFloor: '',
    roomSum: '',
    bedName: '',
    bedWidth: '',
    bedLength: '',
    networkPrice: '',
    windowType: 0,
    networkType: '0',
    roomFacility: '',
    supportAddBed: 0,
    naviDataToBind: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '房型信息'
    })
    const _self = this;
    //房间设备初始化
    wx.request({
      url: 'https://qdndc.ideatower.cn/v1/hotelFacility',
      success: (res) => {
        if (res.data.data.length > 0) {
          let temp = res.data.data;
          temp.forEach((item, index) => {
            if (index == 0) {
              item.checked = true;
            } else {
              item.checked = false;
            }
          })
          _self.setData({
            roomFacilities: temp
          })

          if (options.data != '' && options.data != undefined) {
            const currentData = JSON.parse(options.data);
          
            const tmpList = currentData.networkType.split(';');
            //是否显示收费标准输入框
            tmpList.forEach((item, index) => {
              if (item == 2 || item == 3 || item == 4) {
                _self.setData({
                  isShowNetworkPrice: true
                })
              }
            })
            //绑定网络类型
            _self.data.networkTypes.forEach((item, index) => {
              const findIndex = tmpList.findIndex(o => o == item.value)
              if (findIndex > -1) {
                item.checked = true
              } else {
                item.checked = false
              }
            });

            //绑定设备类型
            const facilityTmp = currentData.roomFacility.split(';')
            _self.data.roomFacilities.forEach((item, index) => {
              const curIndex = facilityTmp.findIndex(o => o == item.id)
              if (curIndex > -1) {
                item.checked = true
              } else {
                item.checked = false
              }
            });

            _self.setData({
              id: currentData.id,
              networkTypes: _self.data.networkTypes,
              roomFacilities: _self.data.roomFacilities,
              belongHotelId: currentData.belongHotelId,
              roomName: currentData.roomName,
              roomArea: currentData.roomArea,
              roomFloor: currentData.roomFloor,
              roomSum: currentData.roomSum,
              bedName: currentData.bedName,
              bedWidth: currentData.bedWidth,
              bedLength: currentData.bedLength,
              networkPrice: currentData.networkPrice,
              windowType: currentData.windowType,
              supportAddBed: currentData.supportAddBed,
              networkType: currentData.networkType,
              roomFacility: currentData.roomFacility,
            });
          }


        } else {
          _self.showZanToast('暂无房间设备信息')
          return
        }
      },
      fail: (err) => {
        _self.showZanToast('获取房间设备信息失败')
        return
      }
    });

    if (options.data != '' && options.data != undefined) {
      const currentData = JSON.parse(options.data);

      const tmpList = currentData.networkType.split(';');
      _self.data.networkTypes.forEach((item, index) => {
        const findIndex = tmpList.findIndex(o => o == item.value)
        if (findIndex > -1) {
          item.checked = true
        } else {
          item.checked = false
        }
      });

      _self.setData({
        id: currentData.id,
        networkTypes: _self.data.networkTypes,
        roomFacilities: _self.data.roomFacilities,
        belongHotelId: currentData.belongHotelId,
        roomName: currentData.roomName,
        roomArea: currentData.roomArea,
        roomFloor: currentData.roomFloor,
        roomSum: currentData.roomSum,
        bedName: currentData.bedName,
        bedWidth: currentData.bedWidth,
        bedLength: currentData.bedLength,
        networkPrice: currentData.networkPrice,
        windowType: currentData.windowType,
        supportAddBed: currentData.supportAddBed,
        networkType: currentData.networkType,
        roomFacility: currentData.roomFacility,
      });
    }
    if (options.id != '' && options != undefined) {
      _self.setData({
        belongHotelId: options.id
      });
    } else {
      wx.showToast({
        title: '获取酒店ID失败',
        icon: 'none',
        duration: 1500
      })
      wx.navigateBack({

      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 窗户类型单选事件
   */
  windowTypeChange: function (e) {
    this.setData({
      windowType: e.detail.value
    });
  },

  /**
   * 上网类型复选事件
   */
  networkTypeChange: function (e) {
    this.setData({
      networkType: e.detail.value.toString().replace(/,/g, ';'),
      isShowNetworkPrice: false
    });
    e.detail.value.forEach((item, index) => {
      if (item == 2 || item == 3 || item == 4) {
        this.setData({
          isShowNetworkPrice: true
        });
      }
    });
  },

  /**
   * 房间设备选择事件
   */
  roomFacilityCheckboxChange: function (e) {
    this.setData({
      roomFacility: e.detail.value.toString().replace(/,/g, ';')
    })
  },

  /**
   * 是否加床单选事件
   */
  supportAddBedChange: function (e) {
    this.setData({
      supportAddBed: e.detail.value
    })
  },

  /**
   * 提交数据
   */
  formSubmit: function (event) {
    //追加数据
    const formData = event.detail.value;
    formData.belongHotelId = this.data.belongHotelId;
    formData.windowType = this.data.windowType;
    formData.networkType = this.data.networkType;
    formData.supportAddBed = this.data.supportAddBed;   
    if (this.data.roomFacility == null || this.data.roomFacility == '' || this.data.roomFacility == undefined) {
      formData.roomFacility = this.data.roomFacilities[0].id;
    } else {
      formData.roomFacility = this.data.roomFacility;
    }
    if (formData.id == undefined || formData.id == null || formData.id == 0) {
      formData.createBy = app.globalData.userInfo.openid;
    } else {
      formData.id = this.data.id;
    }
    formData.updateBy = app.globalData.userInfo.openid;
    for (let key in formData) {
      if (this.data.isShowNetworkPrice == false && key == 'networkPrice') {
        continue
      } else {
        if (formData[key] === '') {
          wx.showToast({
            title: '数据不能为空',
            icon: 'none',
            duration: 2000
          })
          return
        }
      }
    }
    wx.showLoading({
      title: '上传中',
    });
    const _self = this
    let requestUrl = ''
    let method = ''
    if (_self.data.id != '' && _self.data.id != null && _self.data.id != undefined) {
      requestUrl = 'https://qdndc.ideatower.cn/v1/hotelRoom/' + _self.data.id
      method = 'PUT'
    } else {
      requestUrl = 'https://qdndc.ideatower.cn/v1/hotelRoom'
      method = 'POST'
    }

    wx.request({
      url: requestUrl,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: obj2Form(formData),
      method: method,
      success: function (res) {
        if (res.data.code == '000000') {
          _self.showZanToast('成功')
          wx.navigateBack({
            
          })
          return
        }
        if (res.statusCode != 200) {
          _self.showZanToast('请求接口失败')
          return
        }
        _self.showZanToast(res.data.msg)
      },
      fail: function () {
        _self.showZanToast('网络错误')
      },
      complete: function () {
        _self.setData({
          submitDisabled: false
        })
        wx.hideLoading()
      }
    })
  },

}))